home
diamond Go Premium
Data Engineering Path  ·  PySpark

Spark Streaming - Structured Streaming: Sliding Window Workbook

This workbook walks you through tracing sliding window streams with watermarking.


1. Sliding Window Parameters

  • Window Size: 10 minutes
  • Slide Duration: 5 minutes
  • Watermark Delay: 10 minutes

2. Tasks

Task 1: Trace late arrivals

Trace how the streaming engine evaluates state across 3 incoming batches. Map records to windows and track the active watermark threshold.

Task 2: Implement Kafka to Delta Pipeline

Write the PySpark Structured Streaming code to read transactions from Kafka topic payments, parse the JSON payload explicitly, apply a 10-minute watermark on payment_time, and write the output in append mode to a Delta table at /mnt/delta/payments.


3. Step-by-Step Solutions

Solution 1: Stream Tracing Matrix

  • Batch 1 (Incoming):
    • Record A: 12:05:00
    • Windows Triggered: [12:00-12:10], [12:05-12:15]
    • Max Event Time: 12:05:00
    • Active Watermark: None (initialized)
  • Batch 2 (Incoming):
    • Record B: 12:20:00
    • Windows Triggered: [12:15-12:25], [12:20-12:30]
    • Max Event Time: 12:20:00
    • Active Watermark: 12:20:00 - 10 mins = 12:10:00
  • Batch 3 (Incoming - Late):
    • Record C: 12:08:00
    • Evaluation: Is 12:08:00 > Active Watermark (12:10:00)?
    • Decision: No. The record is dropped as late data.

Solution 2: Kafka to Delta Code

from pyspark.sql.functions import from_json, col
from pyspark.sql.types import StructType, StructField, StringType, DoubleType, TimestampType

# Define payment payload schema
payment_schema = StructType([
    StructField("tx_id", StringType(), False),
    StructField("amount", DoubleType(), False),
    StructField("payment_time", TimestampType(), False)
])

# Read from Kafka stream
kafka_stream = spark.readStream \
    .format("kafka") \
    .option("kafka.bootstrap.servers", "localhost:9092") \
    .option("subscribe", "payments") \
    .load()

# Parse JSON and apply watermark
parsed_stream = kafka_stream.selectExpr("CAST(value AS STRING) as json_payload") \
    .select(from_json(col("json_payload"), payment_schema).alias("data")) \
    .select("data.*") \
    .withWatermark("payment_time", "10 minutes")

# Write to transactional Delta Table
query = parsed_stream.writeStream \
    .format("delta") \
    .outputMode("append") \
    .option("checkpointLocation", "/mnt/delta/payments/_checkpoints") \
    .start("/mnt/delta/payments")
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.